﻿using ServerGame.GameObject;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static UtilPublic.Redis.ServerRedis;
using static ServerBase.DataBaseRedis.RdsTxt;
using static ServerBase.Config.Conf;
using static ServerPublic.Data.PubDataMethodMgr;
using UtilPublic.ServerLoger;
using UtilLib;
using ServerBase.Protocol;
using static UtilPublic.Serialization.Jsh;
using ServerPublic.Manager;
using ServerBase.DataBaseRedis;
using ServerBase.Config;
using UtilPublic.Serialization;
using ServerGame.Manager;

namespace ServerGame.Data
{
    //获取玩家数据方式
    public enum EGetPlayerSource
    {
        Online = 0,
        Cache = 1,
        Database = 2,
    };
    /// <summary>
    /// 全局数据
    /// </summary>
    public static partial class DataGame
    {
        #region 全局邮件

        //全局邮件版本号
        public static long GlobalMailsVersion = 0;
        //全局邮件数据
        public static Dictionary<long, Mail> DictGlobalMails = new Dictionary<long, Mail>();
        //存档
        public static void GmailSave(Mail obj)
        {
            DictGlobalMails[obj.Uid] = obj;
            GlobalMailsVersion = IdMgr.CreateObjectId();
            ObjHashSave(RdsMailGlobal, obj.Uid, obj);
        }
        //删除
        public static void GmailRemove(long uid)
        {
            DictGlobalMails.Remove(uid);
            GlobalMailsVersion = IdMgr.CreateObjectId();
            ObjHashRemove(RdsMailGlobal, uid);
        }
        //读取
        public static void GmailLoadAll()
        {
            //读取版本号
            GlobalMailsVersion = IdMgr.CreateObjectId();
            var listuids = ZHashKeys(RdsMailGlobal);
            foreach (long uid in listuids)
            {
                if (ObjHashLoad<Mail>(RdsMailGlobal, uid, out var obj))
                {
                    DictGlobalMails[obj.Uid] = obj;
                }
            }
            loger.Info($"DictGlobalMails {DictGlobalMails.Count}");
        }

        #endregion

        #region 玩家
        //玩家在线列表
        public static Dictionary<long, Player> DictPlayerOnline = new Dictionary<long, Player>();
        //玩家缓存列表
        public static Dictionary<long, Player> DictPlayerCache = new Dictionary<long, Player>();

        // 根据Player唯一ID获取Player
        public static bool GetPlayer(long Puid, out Player player, EGetPlayerSource Source = EGetPlayerSource.Database)
        {
            if (Source >= EGetPlayerSource.Online)
            {
                if (DictPlayerOnline.TryGetValue(Puid, out player))
                {
                    player.SetActive();
                    return true;
                }
            }
            if (Source >= EGetPlayerSource.Cache)
            {
                if (DictPlayerCache.TryGetValue(Puid, out player))
                {
                    player.SetActive();
                    return true;
                }
            }
            if (Source >= EGetPlayerSource.Database)
            {
                player = GetOfflinePlayer(Puid);
                if (null != player)
                {
                    player.SetActive();
                    DictPlayerCache[player.Uid] = player;
                    return true;
                }
                return (player != null);
            }
            player = null;
            return false;
        }
        // 根据Player唯一ID获取Player
        public static Player GetPlayer(long Puid, EGetPlayerSource Source = EGetPlayerSource.Database)
        {
            //如果玩家已在线
            if (!GetPlayer(Puid, out var player, Source))
            {
                loger.Fatal($"DataGlobal.GetPlayer Error {Puid} {Source}");
            }
            return player;
        }
        // 根据Player唯一ID获取离线Player
        public static Player GetOfflinePlayer(long Puid)
        {
            if (!PubPlayerMgr.CheckPlayer(Puid))
            {
                return null;
            }
            var player = new Player(Puid);
            //判断玩家数据是否存在
            if (!player.Load())
            {
                return null;
            }

            return player;
        }
        // 根据Player唯一ID获取Player昵称
        public static string GetPlayerName(long Puid)
        {
            if (Puid <= 0)
            {
                return "";
            }
            if (GetPlayer(Puid, out var ppp))
            {
                return ppp.Name;
            }
            return "";
        }
        #endregion

        #region 城市
        public static Dictionary<string, City> DictCity = new Dictionary<string, City>();
        //读取全部
        public static void CityLoadAll()
        {
            foreach (var conf in ConfCity.Values)
            {
                if (conf.CreateCity(out var obj))
                {
                    DictCity[obj.Uid] = obj;
                }
            }
            loger.Debug($"DictCity:{DictCity.Count}");
        }
        public static bool GetCity(string uid, out City obj)
        {
            return DictCity.TryGetValue(uid, out obj);
        }
        #endregion

        #region 房间
        public static Room BaseRoom = null;
        public static Dictionary<long, Room> DictRoom = new Dictionary<long, Room>();
        //读取全部
        public static void RoomLoadAll()
        {
            foreach (var conf in ConfRoomExt.Values)
            {
                if (conf.RoomCreate(out var obj))
                {
                    if (obj.Cfg.Name.Equals("十字街头"))
                    {
                        BaseRoom = obj;
                    }
                    DictRoom[obj.Uid] = obj;
                }
                loger.Debug($"room: {obj.Uid} 出口:{obj.DictExit.Count}");
                //loger.Debug($"room: {JsonHepler.SerializerSingleData(obj)}");
                //loger.Debug($"room: {obj.Uid}");
                //foreach (var item in obj.DictExit.Values)
                //{
                //    loger.Debug($"dir: {item.RoomUid}");
                //}
            }
            loger.Debug($"DictRoom:{DictRoom.Count} BaseRoomUid:{BaseRoom.Uid}");
        }
        public static bool GetRoom(long uid, out Room obj)
        {
            return DictRoom.TryGetValue(uid, out obj);
        }
        #endregion

        #region 服务器上的玩家ID列表

        //服务器上的玩家ID列表
        public static List<long> ServerListPuid = new List<long>();
        public static void ServerListPuidLoad()
        {
            ZSetMembers(RdsGameListPuid, ref ServerListPuid);
        }
        public static void ServerListPuidAdd(long puid)
        {
            ServerListPuid.Add(puid);
            ZSetAdd(RdsGameListPuid, puid);
        }
        public static bool ServerListPuidIs(long puid)
        {
            return ServerListPuid.Contains(puid);
        }

        #endregion

    }


}
